import { dispatch} from './dispatcher';
import { Session, Application } from 'pinus';

 function fight(session: Session, msg: any, app: Application, cb: (err: Error , serverId ?: string) => void) {

    let servers = app.getServersByType('fight');

    if(!servers || servers.length === 0) {
        cb(new Error('can not find fight servers.'));
        return;
    }

    let res = dispatch("s"+session.get("uid"), servers);

    cb(null, res.id);
}

 function gate(app){
    let connectors = app.getServersByType('connector');
    if (!connectors || connectors.length === 0) {
        return {
            code: 500
        };
    }
    // select connector
    let res = dispatch(Math.random().toString(36).substring(2), connectors);
    return {
        code: 200,
        host: res.host,
        port: res.clientPort
    };
}

export default{
    fight,gate
}